﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.DAL.LocalDataAccess/AlarmRecordAccess
* 类       名: AlarmRecordAccess
* 功      能:  TD
* 时      间:  2024-04-15 14:11:37
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using HslCommunication.Core.IMessage;
using log4net.Util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XrayDirection.Common;
using XrayDirection.DAL.DataEntity;
using XrayDirection.Extensions;

namespace XrayDirection.DAL.LocalDataAccess
{
    public class AlarmRecordAccess
    {
        // 添加一条数据到AlarmRecord表里面去
        public static DataResult AddAlarmRecord(AlarmEntity alarm)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.AlarmRecords.Add(alarm);
                    int affectRows = context.SaveChanges();
                    if (affectRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantManager.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {

                result.Message = ex.GetFullException();
            }
            return result;
        }


        // 获取所有的报警配置代码
        public static DataResult<List<AlarmEntity>> GetAlarms()
        {
            DataResult<List<AlarmEntity>> result = new DataResult<List<AlarmEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.AlarmRecords.ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }


        // 根据AlarmCode获取对应的Alarm信息
        public static DataResult<AlarmEntity> GetAlarmByCode(string alarmCode)
        {
            DataResult<AlarmEntity> result = new DataResult<AlarmEntity>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.AlarmRecords.SingleOrDefault(alarm => alarm.AlarmCode == alarmCode);
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
